package com.jwm.power.service.impl;
import com.jwm.power.mapper.InquiryMapper;
import com.jwm.power.model.common.EnumResult;
import com.jwm.power.model.entity.InquiryT;
import com.jwm.power.model.entity.LogisticsTracking;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.repository.InquiryRepository;
import com.jwm.power.repository.LogisticsRepository;
import com.jwm.power.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;


@Service
@Transactional
public class LogisticsServiceImpl implements LogisticsService {
    @Autowired
    @Qualifier("transactionManager2")
    private PlatformTransactionManager transactionManager2;
    @Autowired
    private LogisticsRepository logisticsRepository;
    @Autowired
    private InquiryMapper inquiryMapper;
    @Autowired
    private InquiryRepository inquiryRepository;
    @Override
    public ResponseData getCompanyName(){
        AtomicReference logisticsSourceResult = new AtomicReference<>();
        try {
            TransactionTemplate template = new TransactionTemplate(transactionManager2);
            template.execute(new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                    //这里是第二数据源
                    List<LogisticsTracking> logisticsTrackings = logisticsRepository.findAll();
                    System.out.println(logisticsTrackings);
                    // 查出所有记录
                    List<InquiryT> inquiryTS = inquiryRepository.findAll();
                    for(InquiryT model_IN : inquiryTS){
                        String country = model_IN.getCountry();
                        String saler = model_IN.getSales();
                        Date startDate = model_IN.getInquirydate();
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(startDate);
                        calendar.add(Calendar.MONTH, -1);
                        Date endDate = calendar.getTime();

                        List<LogisticsTracking> companyList = logisticsRepository.findSJDWByBZAndXSRYAndFJRQBetween(country,saler, startDate, endDate);

                        for(LogisticsTracking company : companyList){
                            Map<String, Object> imap = new HashMap();
                            imap.put("tName", "inquirydata");
                            imap.put("values", company);
                            imap.put("strWhere", " where sales=" + saler + "and inquirydate between "+ startDate +","+endDate);
                            inquiryMapper.updateDefined(imap);
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(EnumResult.Exception.getCode(), e.getMessage(), null);
        }
        return new ResponseData(EnumResult.SUCCESS.getCode(), "success", logisticsSourceResult.get());
    }

}
